-- require("./utils/load_resour")

local string_util = require("string_util")
local log = require("mylog")
local dx_queue = require("dx_queue")
local config = require("config")
local dx_keyboard = require("dx_keyboard")
local dx_gpio = require("dx_gpio")
local os_util = require("os_util")

-- 定义管脚地址
-- 出门开关
local CONFIG_BUTTON_KEY_EXIT = 139
-- 火警输入
local CONFIG_BUTTON_KEY_FIRE = 102
-- 门磁状态
local CONFIG_BUTTON_KEY_SEN = 101

local keyboard_service = {}

-- 初始化keyboard回调队列
function keyboard_service.init()
    local queue = dx_keyboard.init(100)
    while not queue do
        log.error(queue)
        log.error("[设备状态回调] 5秒后重试")
        os.execute("sleep 5")
        queue = dx_keyboard.init(100)
    end
    return queue
end

-- 获取回调状态
function keyboard_service.pop(queue)
    local data = dx_keyboard.pop(queue, 500)
    while not data do
        -- log.error(data)
        -- log.error("[设备状态回调] 5秒后重试")
        os.execute("sleep 1")
        data = dx_keyboard.pop(queue, 500)
    end
    return data
end

-- 释放keyboard
function keyboard_service.destroy(queue)
    local ret, data = dx_keyboard.destroy(queue)
    while ret ~= 1 do
        log.error(data)
        log.error("[设备状态回调] 5秒后重试")
        os.execute("sleep 5")
        ret, data = dx_keyboard.destroy(queue)
    end
end

-- 设备状态回调
function keyboard_service.func_cb(key, pdata)
    if not key or not key.code or not key.value then
        return
    end
    local topic = "20220316/event/alarm" .. "####"
    local msg = ""
    local key_prefix
    if key.code == CONFIG_BUTTON_KEY_EXIT then
        log.info("[设备状态回调] 出门开关状态回调")
        -- 出门开关
        msg = "type@!@2@#@value@!@" .. key.value
        key_prefix = "exit"
    elseif key.code == CONFIG_BUTTON_KEY_FIRE then
        log.info("[设备状态回调] 火警开关状态回调")
        -- 火警输入
        msg = "type@!@1@#@value@!@" .. key.value
        key_prefix = "fire"
    elseif key.code == CONFIG_BUTTON_KEY_SEN then
        log.info("[设备状态回调] 门磁状态回调")
        -- 门磁状态
        msg = "type@!@0@#@value@!@" .. key.value
        key_prefix = "sen"
    else
        log.info("[设备状态回调] 没有找到该回调类型，code：" .. key.code .. ",value：" .. key.value)
        return nil
    end
    local flag = keyboard_service.func_interval(key_prefix, key.value)
    if flag then
        log.info(topic .. msg)
        dx_queue.push(mqtt_queue, topic .. msg, 500)
    end
end

-- 回调逻辑
function keyboard_service.func_interval(key_prefix, value)
    local flag = false
    if not key_prefix or not value then
        return flag
    end
    -- 记录时间条件：每次关状态时或者上送时，状态上送条件：与上次时间大于5s且状态较上次发生变化，更新状态条件：上送
    config.configStrToTable()
    if (os.time() - g_config[key_prefix .. "_time"] > 5) and value ~= g_config[key_prefix .. "_status"] then
        flag = true
        g_config[key_prefix .. "_time"] = os.time()
        g_config[key_prefix .. "_status"] = value
    end
    if value == 0 then
        g_config[key_prefix .. "_time"] = os.time()
    end
    config.configTableToStr()
    return flag
end
_G._G_map = _G["_G_map"]
_G.mqtt_queue = dx_map.get(_G_map, "mqtt_queue")

local queue = keyboard_service.init()

while true do
    local data = keyboard_service.pop(queue)
    if data then
        print("=============", data)
        local key_value = string_util.split(data, ",")
        keyboard_service.func_cb({code = tonumber(key_value[1]), value = tonumber(key_value[2])}, nil)
    end
    -- 网络灯闪烁逻辑
    config.configStrToTable()
    if g_config["network_status"] and not g_config["mqtt_con_status"] then
        dx_gpio.gpio_set_value(GPIO_73, 1)
        os_util.sleep(0.3)
        dx_gpio.gpio_set_value(GPIO_73, 0)
        os_util.sleep(0.01)
    end
end

return keyboard_service
